package com.txzt.gaea.base.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.aspect.annotation.SysDataPermission;
import com.txzt.gaea.business.fund.service.IFundApplyService;
import com.txzt.gaea.business.fund.service.IFundBudgetService;
import com.txzt.gaea.business.ident.domain.model.IdentInfo;
import com.txzt.gaea.business.ident.service.IIdentInfoService;
import com.txzt.gaea.business.plan.domain.query.PlanInfoQuery;
import com.txzt.gaea.business.plan.service.IPlanInfoService;
import com.txzt.gaea.business.project.domain.model.ProjectFundExpenseRequest;
import com.txzt.gaea.business.project.service.IProjectFERService;
import com.txzt.gaea.business.project.service.IProjectInfoService;
import com.txzt.gaea.business.project.service.ProjectContractService;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.contract.service.BizContractService;
import com.txzt.gaea.upgrade.out.domain.model.OutContract;
import com.txzt.gaea.upgrade.out.domain.model.OutCoop;
import com.txzt.gaea.upgrade.out.service.IOutContractService;
import com.txzt.gaea.upgrade.out.service.IOutCoopService;
import com.txzt.gaea.upgrade.project.domain.model.BudgetManage;
import com.txzt.gaea.upgrade.project.domain.model.ProjectMain;
import com.txzt.gaea.upgrade.project.service.IBudgetManageService;
import com.txzt.gaea.upgrade.project.service.IProjectMainService;
import com.txzt.gaea.upgrade.result.domain.model.ResultBorrow;
import com.txzt.gaea.upgrade.result.domain.model.ResultInfo;
import com.txzt.gaea.upgrade.result.service.IResultBorrowService;
import com.txzt.gaea.upgrade.result.service.IResultInfoService;
import com.txzt.gaea.vo.AdminUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/home/page/statistics")
public class HomePageStatisticsController extends BaseController {
    @Autowired
    private IPlanInfoService iPlanInfoService;
    @Autowired
    private IProjectInfoService iProjectInfoService;
    @Autowired
    private IFundBudgetService iFundBudgetService;
    @Autowired
    private IFundApplyService iFundApplyService;
    @Autowired
    private BizContractService bizContractService;
    @Autowired
    private ProjectContractService projectContractService;

    @GetMapping(value = "/info")
    public RtnInfo getStatistics(){
        Map<String,Object> map = new HashMap<>();
        //计划相关统计
        map.put("planTotal",iPlanInfoService.getTotal());
        map.put("planInfo",iPlanInfoService.getNumByStatus());
        //项目相关
        map.put("projectInfo",iProjectInfoService.getProjectNum());
        //经费相关
        map.put("fundSum",iFundBudgetService.getFundSum());
        map.put("fundApply",iFundApplyService.getTotalNum());
        //业务合同
        map.put("bizContract",bizContractService.getTotalNum());
        //项目合同
        map.put("projectContract",projectContractService.getTotalNum());
        return RtnInfo.success(map);
    }

    @Autowired
    private IPlanInfoService planInfoService;
    @Autowired
    private IProjectMainService projectMainService;
    @Autowired
    private IOutCoopService outCoopService;
    @Autowired
    private IOutContractService outContractService;
    @Autowired
    private IProjectFERService iProjectFERService;
    @Autowired
    private IBudgetManageService budgetManageService;
    @Autowired
    private IIdentInfoService identInfoService;
    @Autowired
    private IResultInfoService resultInfoService;
    @Autowired
    private IResultBorrowService resultBorrowService;


    /**
     * 查询待审核数量(一级菜单统计)
     * @param
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/listCheckCountSum")
    public RtnInfo<?> listCheckCountSum() {
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        Long memberId = adminUserVO.getId();
        Map<String,Long> map = new HashMap<>();
        PlanInfoQuery planInfoQuery = new PlanInfoQuery();
        planInfoQuery.setPlanStatus(1);
        planInfoQuery.setCurrentStepMember(memberId);
        Long count1 = planInfoService.listCheckCount(planInfoQuery);
        map.put("计划审核",count1);
        map.put("计划拟制",count1);
        ProjectMain projectMain = new ProjectMain();
        projectMain.setCurrentStepMember(memberId);
        projectMain.setGlobalStatuses(Arrays.asList(112,124,134));
        Long count2 = projectMainService.listCheckCount(projectMain);
        map.put("开题报告管理",count2);
        projectMain.setGlobalStatuses(Arrays.asList(214,224,234));
        Long count3 = projectMainService.listCheckCount(projectMain);
        map.put("结题管理",count3);
        OutCoop outCoop = new OutCoop();
        outCoop.setCurrentStepMember(memberId);
        Long count4 = outCoopService.listCheckCount(outCoop);
        map.put("外协合作",count4);
        OutContract outContract = new OutContract();
        outContract.setCurrentStepMember(memberId);
        Long count5 = outContractService.listCheckCount(outContract);
        map.put("外协合同",count5);
        ProjectFundExpenseRequest projectFund = new ProjectFundExpenseRequest();
        projectFund.setCurrentStepMember(memberId);
        Long count6 = iProjectFERService.listCheckCount(projectFund);
        map.put("项目过程经费支出申请",count6);
        projectMain.setGlobalStatuses(Arrays.asList(887));
        Long count7 = projectMainService.listCheckCount(projectMain);
        map.put("延期完成申请",count7);
        projectMain.setGlobalStatuses(Arrays.asList(998));
        Long count8 = projectMainService.listCheckCount(projectMain);
        map.put("终止研究申请",count8);
        BigDecimal add1 = new BigDecimal(count2).add(new BigDecimal(count3)).add(new BigDecimal(count4)).add(new BigDecimal(count5)).add(new BigDecimal(count6)).add(new BigDecimal(count7)).add(new BigDecimal(count8));
        map.put("过程管理",add1.longValue());
        BudgetManage budgetManage = new BudgetManage();
        budgetManage.setCurrentStepMember(memberId);
        Long count9 = budgetManageService.listCheckCount(budgetManage);
        map.put("经费预算",count9);
        map.put("经费管理",count9);
        IdentInfo identInfo = new IdentInfo();
        identInfo.setCurrentStepMember(memberId);
        Long count10 = identInfoService.listCheckCount(identInfo);
        map.put("鉴定管理",count10);
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCurrentStepMember(memberId);
        Long count11 = resultInfoService.listCheckCount(resultInfo);
        map.put("成果申请",count11);
        ResultBorrow resultBorrow = new ResultBorrow();
        resultBorrow.setCurrentStepMember(memberId);
        Long count12 = resultBorrowService.listCheckCount(resultBorrow);
        map.put("成果借阅管理",count12);
        BigDecimal add2 = new BigDecimal(count11).add(new BigDecimal(count12));
        map.put("成果管理",add2.longValue());
        return RtnInfo.success(map);
    }
}
